﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.commands;
using InHD.utilities.enums;
using InHD.dataaccess;

namespace InHD.viewmodels
{
    class CustomerFormViewModel : WorkspaceViewModel, IDataErrorInfo
    {
        #region Constructor
        public CustomerFormViewModel(ActionModes mode, Customer customer)
        {
            FormMode = mode;
            _customer = customer;
            if (FormMode.Equals(ActionModes.Edit))
            {
                TransactionRepository repo = new TransactionRepository();
                this.TransactionRowCollection = repo.GetTransactionByCustomer(_customer.CustomerUNK);
            }
        }
        #endregion

        List<Transaction> _trans;

        public List<Transaction> TransactionRowCollection
        {
            get
            {
                return this._trans;
            }
            set
            {
                if (value != null)
                {
                    this._trans = value;
                    OnPropertyChanged("TransactionRowCollection");
                }
            }
        }

        #region Value Property
        private Customer _customer;
        public string commandExcute = string.Empty;
        bool CanDelete
        {
            get { return !string.IsNullOrEmpty(this._customer.CustomerUNK) ? true : false; }
        }

        public ActionModes FormMode
        {
            get;
            set;
        }

        #endregion

        #region Presentation Property
        public override string DisplayName
        {
            get
            {
                return "Customer";
            }
            protected set
            {
                base.DisplayName = value;
            }
        }

        public Customer Customer
        {
            get
            {
                return _customer;
            }
        }

        public string CustomerCode
        {
            get { return _customer.CustomerCode; }
            set
            {
                if (value == _customer.CustomerCode)
                    return;

                _customer.CustomerCode = value;

                base.OnPropertyChanged("CustomerCode");
            }
        }

        public string LastName
        {
            get { return _customer.LastName; }
            set
            {
                if (value == _customer.LastName)
                    return;

                _customer.LastName = value;

                base.OnPropertyChanged("LastName");
            }
        }

        public string FirstName
        {
            get { return _customer.FirstName; }
            set
            {
                if (value == _customer.FirstName)
                    return;

                _customer.FirstName = value;

                base.OnPropertyChanged("FirstName");
            }
        }

        public string Company
        {
            get { return _customer.Company; }
            set
            {
                if (value == _customer.Company)
                    return;

                _customer.Company = value;

                base.OnPropertyChanged("Company");
            }
        }

        public string Position
        {
            get { return _customer.Position; }
            set
            {
                if (value == _customer.Position)
                    return;

                _customer.Position = value;

                base.OnPropertyChanged("Position");
            }
        }

        public string Phone
        {
            get { return _customer.Phone; }
            set
            {
                if (value == _customer.Phone)
                    return;

                _customer.Phone = value;

                base.OnPropertyChanged("Phone");
            }
        }

        public string Fax
        {
            get { return _customer.Fax; }
            set
            {
                if (value == _customer.Fax)
                    return;

                _customer.Fax = value;

                base.OnPropertyChanged("Fax");
            }
        }

        public string Email
        {
            get { return _customer.Email; }
            set
            {
                if (value == _customer.Email)
                    return;

                _customer.Email = value;

                base.OnPropertyChanged("Email");
            }
        }

        public string Address
        {
            get { return _customer.Address; }
            set
            {
                if (value == _customer.Address)
                    return;

                _customer.Address = value;

                base.OnPropertyChanged("Address");
            }
        }


        #endregion

        #region IDataErrorInfo Interface
        string IDataErrorInfo.Error
        {
            get { return (_customer as IDataErrorInfo).Error; }
        }

        string IDataErrorInfo.this[string propertyName]
        {
            get
            {
                string error = null;

                error = (_customer as IDataErrorInfo)[propertyName];


                // Dirty the commands registered with CommandManager,
                // such as our Save command, so that they are queried
                // to see if they can execute now.
                CommandManager.InvalidateRequerySuggested();

                return error;
            }
        }
        #endregion

        #region Command
        private RelayCommand _closeCommand;
        public ICommand CancelCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new RelayCommand(
                        (param) => this.Close());
                }
                return _closeCommand;
            }
        }

        private RelayCommand _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(
                        (param) => this.Save(), (param) => this._customer.IsValid);
                }
                return _saveCommand;
            }
        }

        private RelayCommand _deleteCommand;
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    _deleteCommand = new RelayCommand(
                        (param) => this.Delete(), (param)=>this.CanDelete);
                }
                return _deleteCommand;
            }
        }
        #endregion

        #region Command Method
        void Close()
        {
            commandExcute = "Close";
            OnRequestClose();
        }

        void Save()
        {
            commandExcute = "Save";
            OnRequestClose();
        }

        void Delete()
        {
            commandExcute = "Delete";
            OnRequestClose();
        }
        #endregion

    }
}
